<?php

/*
Plugin Name: TM Shortcode
Plugin URL: http://tmlife.net/?p=2742
Description: ショートコード集
Version: 1.0
Author: phi
Author URI: http://tmlife.net
*/


add_action('after_setup_theme', 'setup_tm_shortcode');

function setup_tm_shortcode()
{
    /**
     * hello world
     */
    function helloFunc() {
        return "<p><strong>Hello, world!</strong></p>";
    }
    add_shortcode('hello', 'helloFunc');
    

    /**
     * code 
     */
    function codeFunc($attr, $content = null) {
        $content = htmlspecialchars( $content );
        return $content;
    }
    add_shortcode('code', 'codeFunc');
    
    
    /**
     * custom
     */
    function customFunc($attr) {
        extract(shortcode_atts(array(
            'name' => '',
            'index' => 0
        ), $attr));
        
        if ($name == '') return '';
        
        $values = post_custom($name);
        if (is_string($values)) {
            return $values;
        }
        else if (is_array($values)) {
            return $values[ $index ];
        }
        else {
            return "#";
        }
    }
    add_shortcode('custom', 'customFunc');
    
    
    /**
     * link
     */
    function linkFunc($attr, $content=null)
    {
        extract(shortcode_atts(array(
            'href' => '#'
        ), $attr));
        
        if ($content == null) {
            $html = file_get_contents($href);
            if (preg_match("/<title>(.*?)<\/title>/i", $html, $matches)) {
                $content = $matches[1];
            }
            else {
                $content = $href;
            }
        }
        
        return '<a href="' . $href . '">' . $content . '</a>';
    }
    add_shortcode('link', 'linkFunc');

}


?>